_require "../../basis.smi"
_require "./builtintypes/timestamp/TimeStamp.smi"
_require local "./SQLErrors.smi"
_require "./SQLBackendTy.smi"
_require "./Numeric.smi"
_require local "./PGSQLBackend.smi"
_require local "./MySQLBackend.smi"
_require local "./ODBCBackend.smi"
_require "./SQLite3.smi"
_require local "./SQLite3Backend.smi"
_require local "./SQLBACKEND.sig"

structure SMLSharp_SQL_Backend =
struct

  datatype res_impl =
       R of {closeRes : unit -> unit,
             fetch : unit -> bool,
             getInt : int -> int option,
             getIntInf : int -> IntInf.int option,
             getWord : int -> word option,
             getReal : int -> real option,
             getReal32 : int -> real32 option,
             getString : int -> string option,
             getChar : int -> char option,
             getBool : int -> bool option,
             getTimestamp : int -> SMLSharp_SQL_TimeStamp.timestamp option,
             getNumeric : int -> SMLSharp_SQL_Numeric.num option}

  type conn_impl =
       {closeConn : unit -> unit,
        getDatabaseSchema : unit -> SMLSharp_SQL_BackendTy.schema,
        columnTypeName : SMLSharp_SQL_BackendTy.ty -> string,
        execQuery : string -> res_impl}

  type server_impl =
       {connect : unit -> conn_impl}

  datatype backend = BACKEND of server_impl

  val postgresql : string -> backend
  val mysql : string -> backend
  val odbc : string -> backend
  val sqlite3 : string -> backend
  val sqlite3' : SMLSharp_SQL_SQLite3.flags * string -> backend
  val default : string -> backend

  structure SQLite3 =
  struct
    val flags = SMLSharp_SQL_SQLite3.flags
    type open_mode = SMLSharp_SQL_SQLite3.open_mode
    val SQLITE_OPEN_READONLY = SMLSharp_SQL_SQLite3.SQLITE_OPEN_READONLY
    val SQLITE_OPEN_READWRITE = SMLSharp_SQL_SQLite3.SQLITE_OPEN_READWRITE
    val SQLITE_OPEN_READWRITE_CREATE =
        SMLSharp_SQL_SQLite3.SQLITE_OPEN_READWRITE_CREATE
    type threading_mode = SMLSharp_SQL_SQLite3.threading_mode
    val SQLITE_OPEN_NOMUTEX = SMLSharp_SQL_SQLite3.SQLITE_OPEN_NOMUTEX
    val SQLITE_OPEN_FULLMUTEX = SMLSharp_SQL_SQLite3.SQLITE_OPEN_FULLMUTEX
    type cache_mode = SMLSharp_SQL_SQLite3.cache_mode
    val SQLITE_OPEN_SHAREDCACHE = SMLSharp_SQL_SQLite3.SQLITE_OPEN_SHAREDCACHE
    val SQLITE_OPEN_PRIVATECACHE = SMLSharp_SQL_SQLite3.SQLITE_OPEN_PRIVATECACHE
    type uri_mode = SMLSharp_SQL_SQLite3.uri_mode
    val SQLITE_OPEN_URI = SMLSharp_SQL_SQLite3.SQLITE_OPEN_URI
  end

end
